﻿using KL.Store.Core.iodeme;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace KL.Store.Core.POS
{
    public class ArenaHelper
    {

        private iodeme.iodeme myIOdeme;
        private iodeme.Init3DSecureTransactionResponse init3DsecureTransactionResult;


        private string inputType = "text"; //text veya hidden

        private string errorMsg = "";


        public string Inputs(string agentID, string userName, string url, string bankID, string posID, string cardNo, string cardHolderName, int instalment, decimal amount, string currency, int cardExpireYear, int cardExpireMonth, int cardExpireDay, string cvc, string email, string phone, string companyName, string returnURL)
        {

            string returnValue = "";

            returnValue += "  <form id=\"form1\" method=\"post\" style=\"display:none;\" action={0} runat=\"server\">";
            returnValue += "      <div runat=\"server\" id=\"formElemanlari\">";
            if (Cal(agentID, userName, url))
            {

                Init3DsecureTransaction(bankID, posID, cardNo, cardHolderName, instalment, amount, currency, cardExpireYear, cardExpireMonth, cardExpireDay, cvc, email, phone, companyName, returnURL);
                if (init3DsecureTransactionResult.hiddenfields != null)
                    foreach (XmlNode currentNode in init3DsecureTransactionResult.hiddenfields.ChildNodes)
                    {
                        returnValue += currentNode.Name + " : <input type=\"" + inputType + "\" id=\"" + currentNode.Name + "\" name=\"" + currentNode.Name + "\" value=\"" + currentNode.InnerText + "\" /><br/>";
                    }
                if (init3DsecureTransactionResult.Action != "")
                {
                    returnValue = string.Format(returnValue, init3DsecureTransactionResult.Action);

                }
            }
            returnValue += "</div><input type=\"submit\" id=\"submitButton\" value=\"DEVAM\" /><br />";
            returnValue += "</form>";

            returnValue += "<script>";
            returnValue += " $(\"#form1\").submit();";
            returnValue += "</script>";
            return returnValue;
        }

        private void Init3DsecureTransaction(string bankID, string posID, string cardNo, string cardHolderName, int instalment, decimal amount, string currency, int cardExpireYear, int cardExpireMonth, int cardExpireDay, string cvc, string email, string phone, string companyName, string returnURL)
        {
            iodeme.xactParam3d[] xactParams3d = new iodeme.xactParam3d[1];
            xactParams3d[0] = new iodeme.xactParam3d();

            xactParams3d[0].bankID = bankID;
            xactParams3d[0].posID = posID;
            xactParams3d[0].cardno = cardNo;

            xactParams3d[0].cardholdername = cardHolderName;

            xactParams3d[0].instalment = instalment == 1 ? 0 : instalment;
            xactParams3d[0].amount = amount;
            xactParams3d[0].currency = currency;

            xactParams3d[0].cardexpiredate = new DateTime(cardExpireYear, cardExpireMonth, cardExpireDay);
            xactParams3d[0].cvc = cvc;

            xactParams3d[0].email = email;
            xactParams3d[0].phone = phone;

            xactParams3d[0].companyname = companyName;

            xactParams3d[0].returnURL = returnURL;

            try
            {
                init3DsecureTransactionResult = myIOdeme.Init3DSecureTransaction(xactParams3d[0]);
            }
            catch (Exception ex)
            {
                init3DsecureTransactionResult = new Init3DSecureTransactionResponse();
                init3DsecureTransactionResult.ErrorCode = "Neticaret";
                init3DsecureTransactionResult.ErrorText = "Pos bilgileri alınırken bir hata oluştu. Hata : " + ex.Message;
                init3DsecureTransactionResult.Action = "";
                errorMsg = ex.Message;
            }
        }

        private bool Cal(string myAgentID, string myUserName, string url)
        {
            bool returnValue = true;
            try
            {
                myIOdeme = new iodeme.iodeme();
                myIOdeme.Url = url;
                iodeme.AuthenticationInfo aInfo = new iodeme.AuthenticationInfo();

                aInfo.agentID = myAgentID;
                aInfo.username = myUserName;
                myIOdeme.AuthenticationInfoValue = aInfo;
            }
            catch (Exception Ex)
            {
                returnValue = false;
                errorMsg = Ex.Message;
            }
            return returnValue;
        }


        public static string FormRequest(string variable, string defaultValue = "")
        {
            return HttpContext.Current.Request.Form[variable] != null ? HttpContext.Current.Request.Form[variable].ToString() : defaultValue;
        }
        public xactResult Complete3dT(string myAgentID, string myUserName, string url, string bankID, string posID)
        {
            SortedList requestFields = new SortedList();
            foreach (string item in HttpContext.Current.Request.Form)
            {
                requestFields.Add(item, FormRequest(item));
            }
            if (Cal(myAgentID, myUserName, url))
            {
                return Complete3DsecureTransaction(bankID, posID, requestFields);
            }
            else
            {
                return new xactResult();
            }
        }
        public xactResult Complete3DsecureTransaction(string bankID, string posID, SortedList requestFields)
        {
            iodeme.xactResult myActResult = null;
            iodeme.NameValue[] requestFieldList = new iodeme.NameValue[requestFields.Count];
            try
            {

                int i = 0;
                foreach (string key in requestFields.Keys)
                {
                    requestFieldList[i] = new iodeme.NameValue();
                    requestFieldList[i].name = key;
                    requestFieldList[i].value = requestFields[key].ToString();
                    i++;
                }


            }
            catch (Exception Ex)
            {
                errorMsg = Ex.Message;
            }
            finally
            {
                //Response.Write("Hata : " + errorMsg);
            }

            return myIOdeme.Complete3DSecureTransaction(bankID, posID, requestFieldList);
        }
    }
}
